package com.xxs.controller.admin;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.xxs.common.Const;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.UUIDUtils;
import com.xxs.utils.xml.Fields;
import com.xxs.utils.xml.XmlDom4jUtils;

@Before(AdminInterceptor.class)
@SuppressWarnings("unchecked")
public class ModelController extends BaseAdminSiteController {

	public void list(){
		String name = getPara(0);
		if(name==null){
			name = "info";
		}
		String modelName = getModeName();
		Element element = XmlDom4jUtils.getRootElement(getXmlPath(getCurrentSiteCode(), modelName));
		List<Element> list = element.elements(name);
		setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name));
    	setAttr("list", list);
    	render("list.html");
    }
	
	public void add(){
		String name = getPara(0);
		setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name));
		setAttr("name", name);
		render("add.html");
	}
	public void addFields(){
		String name = getPara(0);
		String id = getPara(1);
    	String modelName = getModeName();
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
		Element element = null;
		List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
		List<Element> oldlist =  element.elements();
		List<Element> newlist = new ArrayList<Element>();
		List<Element> list = XmlDom4jUtils.getElements(Const.INFO_FIELDS_PATH,null);
		for (int i = 0; i < list.size(); i++) {
			boolean state = true;
			for (int j = 0; j < oldlist.size(); j++) {
				if(oldlist.get(j).getName().equals(list.get(i).getName())){
					state = false;
				}
			}
			if(state){
				newlist.add(list.get(i));
			}
		}
		setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_ATTR_PATH,null));
		setAttr("list", newlist);
		setAttr("name", name);
		setAttr("id", id);
		render("addFields.html");
	}
	public void save(){
		String name = getPara(0);
		String modelName = getModeName();
		List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name);
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(),modelName));
		Element element = document.getRootElement();
		Element newElement = element.addElement(name);
		newElement.setName(name);
		for (int i = 0; i < fieldList.size(); i++) {
			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
			if("id".equals(fieldList.get(i).getName())){
				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
			}else{
				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
			}
		}
		try {
			XmlDom4jUtils.saveDocumentToFile(getXmlPath(getCurrentSiteCode(),modelName), document);
		} catch (Exception e) {
			e.printStackTrace();
		}
		list();
	}
	public void saveFields(){
		String name = getPara(0);
		String id = getPara(1);
		String[] names  = getParaValues("res");
    	String modelName = getModeName();
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
		Element element = null;
		List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
		for (int i = 0; i < names.length; i++) {
			List<Element> list = XmlDom4jUtils.getElements(Const.INFO_FIELDS_PATH,names[i]);
			if(list!=null && list.size()>0){
				element.add(list.get(0));
			}
		}
//		String modelName = getModeName();
//		List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name);
//		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(),modelName));
//		Element element = document.getRootElement();
//		Element newElement = element.addElement(name);
//		newElement.setName(name);
//		for (int i = 0; i < fieldList.size(); i++) {
//			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
//			if("id".equals(fieldList.get(i).getName())){
//				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
//			}else{
//				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
//			}
//		}
		try {
			XmlDom4jUtils.saveDocumentToFile(getXmlPath(getCurrentSiteCode(),modelName), document);
		} catch (Exception e) {
			e.printStackTrace();
		}
		list();
	}
    public void listFields(){
    	String name = getPara(0);
    	String id = getPara(1);
    	String modelName = getModeName();
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
		Element element = null;
		List<Fields> list = XmlDom4jUtils.getFields(Const.SYSTEM_ATTR_PATH,null);
		List<Element> listElements = document.selectNodes("/"+modelName+"s/"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
    	setAttr("fieldList", list);
		for (int i = 0; i < element.elements().size(); i++) {
			Element e = (Element)element.elements().get(i);
			e.getName();
			System.out.println(e.getName()+"----"+e.attributeValue("type")+"9999"+e.attributes());
		}
		setAttr("name", name);
		setAttr("id", id);
		setAttr("elements", element);
    	render("listFields.html");
    }
    public void edit(){
    	String name = getPara(0);
    	String id = getPara(1);
    	setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name));
    	Element element = null;
    	Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), getModeName()));
    	List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
    	setAttr("id", id);
    	setAttr("name", name);
    	setAttr("element", element);
    	render("edit.html");
    }
	public void update(){
		String name = getPara(0);
		String id = getPara(1);
		String modelName = getModeName();
    	Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
    	List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
    	Element element = null;
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    		Enumeration<String> enumeration = getParaNames();
        	while(enumeration.hasMoreElements()){
        		String ename=(String)enumeration.nextElement();
        		String value=getPara(ename);
        		System.out.print(ename + "=" + value);
        		Attribute attribute = element.attribute(ename);  
        		if(attribute==null){
        			element.addAttribute(ename, value);
        		}else{
        			attribute.setValue(value);
        		}
        	}
        	try {
        		XmlDom4jUtils.saveDocumentToFile(getXmlPath(getCurrentSiteCode(), modelName), document);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
    	}
    	list();
	}
	public void editFields(){
		String name = getPara(0);
		String id = getPara(1);
		setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_MODEL_PATH,name));
		Element element = null;
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), getModeName()));
		List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
			element = listElements.get(0);
		}
		setAttr("id", id);
		setAttr("name", name);
		setAttr("element", element);
		render("edit.html");
	}
}
